package com.iwhalecloud.bss.kite.cucc.order.util;

import com.iwhalecloud.bss.kite.client.dto.cust.CustDTO;
import com.iwhalecloud.bss.kite.client.dto.rule.common.DataRuleResult;
import com.iwhalecloud.bss.kite.common.callclient.service.rule.IRuleTriggerService;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.RuleResponseUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.order.client.context.OrderContext;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.rul.core.client.dto.common.RuleCommonParam;
import com.ztesoft.bss.rul.core.client.dto.kite.KiteRuleResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新转单-规则触发工具类
 *
 * @author huang.haizhou
 *
 */
public abstract class RuleTriggerUtil {

    public RuleTriggerUtil() {
    }

    public static IQuerySceneInstService getQuerySceneInstService() {
        return SpringUtil.getBean(IQuerySceneInstService.class);
    }

    public static IOperateSceneInstService getOperateSceneInstService() {
        return SpringUtil.getBean(IOperateSceneInstService.class);
    }

    public static DataRuleResult bfOrderSaveRule(String custOrderId, String custId,
            List<Map<String, Object>> orderInfoMap, OrderRequest orderRequest) {

        IRuleTriggerService ruleTriggerService = SpringUtil.getBean(IRuleTriggerService.class);
        List<String> sceneInstIds = orderRequest.getSceneInstIds();

        Map<String, List<String>> sceneList = RuleTriggerUtil.getSceneListMap(sceneInstIds, custId);
        List<String> serviceOfferIds = sceneList.get("serviceOfferIdList");

        RuleCommonParam ruleCommonParam = RuleTriggerUtil.getRuleParam(orderRequest);

        KiteRuleResponse ruleResponse = ruleTriggerService.onBfOrderSaveRule(sceneInstIds, serviceOfferIds,
                orderInfoMap, ruleCommonParam);

        return RuleResponseUtils.transRuleResponse2RuleResult(ruleResponse);
    }

    public static DataRuleResult afOrderSaveRule(String custOrderId, String custId,
            List<Map<String, Object>> orderInfoMap, OrderRequest orderRequest) {

        IRuleTriggerService ruleTriggerService = SpringUtil.getBean(IRuleTriggerService.class);
        List<String> sceneInstIds = orderRequest.getSceneInstIds();

        Map<String, List<String>> sceneList = RuleTriggerUtil.getSceneListMap(sceneInstIds, custId);
        List<String> serviceOfferIds = sceneList.get("serviceOfferIdList");

        RuleCommonParam ruleCommonParam = RuleTriggerUtil.getRuleParam(orderRequest);

        KiteRuleResponse ruleResponse = ruleTriggerService.onAfOrderSaveRule(sceneInstIds, serviceOfferIds,
                orderInfoMap, ruleCommonParam);

        return RuleResponseUtils.transRuleResponse2RuleResult(ruleResponse);
    }

    private static Map<String, List<String>> getSceneListMap(List<String> sceneInstIds, String custId) {

        Map<String, List<String>> result = new HashMap<>();
        List<String> serviceOfferIds = new ArrayList<String>();

        for (String sceneInstId : sceneInstIds) {
            KiteSceneInst kiteSceneInst = RuleTriggerUtil.getQuerySceneInstService().getSceneInst(sceneInstId, custId);
            serviceOfferIds.add(kiteSceneInst.getServiceOfferId());
        }
        result.put("sceneInstIdList", sceneInstIds);
        result.put("serviceOfferIdList", serviceOfferIds);

        return result;
    }

//    /**
//     * 通过客户订单id获取所有场景实例id
//     * @param custOrderId
//     * @return
//     */
//    private static List<String> getSceneInstIdsByCustOrderId(String custOrderId) {
//        List<String> list =  new ArrayList<>();
//        list.add(custOrderId);
//        // 等肖boss提供给API
//        return list;
//    }

    @SuppressWarnings("deprecation")
    public static RuleCommonParam getRuleParam(OrderRequest orderRequest) {
        RuleCommonParam param = new RuleCommonParam();
        CustDTO cust = OrderContext.getCust();
        param.setLanId(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        param.setOrgId(ContextUtil.getSimpleLoginInfo().getOrgId().toString());
        param.setRegionId(ContextUtil.getLoginInfo().getUserInfo().getPostRegionId().toString());
        param.setSysUserId(ContextUtil.getUserId().toString());
        param.setCustId(cust.getCustId());

        param.setAcceptMode(orderRequest.getAcceptMode());

        com.ztesoft.bss.rul.core.client.dto.common.ContextCustDTO ruleCust = new com.ztesoft.bss.rul.core.client.dto.common.ContextCustDTO();
        ruleCust.setCustId(cust.getCustId());
        ruleCust.setCustName(cust.getCustName());
        ruleCust.setCertType(cust.getCertType());
        if (!KiteListUtils.isEmpty(cust.getPartyCertDTO())) {
            ruleCust.setCertNum(cust.getPartyCertDTO().get(0).getCertNum());
        }
        ruleCust.setLanId(cust.getLanId());
        ruleCust.setRegionId(cust.getRegionId());
        if (!KiteListUtils.isEmpty(cust.getContacts())) {
            ruleCust.setContactName(cust.getContacts().get(0).getContactName());
        }
        ruleCust.setMobilePhone(cust.getMobilePhone());
        param.setCustInfo(ruleCust);
        return param;
    }

}
